package com.liwbn.rbac.controller;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.liwbn.rbac.exception.JsonReException;
import com.liwbn.rbac.model.Framemenu;
import com.liwbn.rbac.service.FramemenuService;
import com.liwbn.rbac.support.ExceptionCode;
import com.liwbn.rbac.support.JsonReInfo;
import com.liwbn.rbac.vo.SideBar;
import com.liwbn.rbac.vo.UserVO;

/**
 * 系统模块管理
 * 
 * @author liwb 2016年3月4日 下午6:50:13
 */
@Controller
public class SystemManageController {

    @Autowired
    private FramemenuService framemenuService;

    /**
     * 系统模块管理
     * 
     * @return
     */
    @RequestMapping("/system/sysm")
    @RequiresPermissions("sysm:view")
    public String sysm() {
        return "system/sysm";
    }

    /**
     * 获取菜单列和头信息
     * 
     * @return
     */
    @RequestMapping("/sideBar")
    @ResponseBody
    public String sideBar() {
        List<SideBar> menus;
        try {
            Subject subject = SecurityUtils.getSubject();
            String userName = (String)subject.getPrincipal();
            menus = framemenuService.selectSideBarByRole(userName);
        } catch (Exception e) {
            throw new JsonReException(101, ExceptionCode.CODE_101_EXCEPTION, e);
        }
        return new JsonReInfo(100, menus).toString();
    }

    @RequestMapping("/system/getAllMenus")
    @ResponseBody
    public String getAllMenus() {
        List<Framemenu> menus;
        try {
            menus = framemenuService.selectAll();
        } catch (Exception e) {
            throw new JsonReException(101, ExceptionCode.CODE_101_EXCEPTION, e);
        }
        return new JsonReInfo(100, menus).toString();
    }

    /**
     * 根据客户端提交信息修改菜单
     * 
     * @param menus
     *            格式: [[a][u][d]]
     * @return
     */
    @RequestMapping("/system/changeMenus")
    @ResponseBody
    public String parseMenus(String menus) {
        try {
            if (StringUtils.isNotBlank(menus)) {
                ObjectMapper om = new ObjectMapper();
                List<List<Framemenu>> tempFrame = om.readValue(menus,
                        new TypeReference<List<List<Framemenu>>>() {
                        });
                if (tempFrame.size() == 3) {
                    framemenuService.updateByJSONMenus(tempFrame);
                }
                return new JsonReInfo(100, ExceptionCode.CODE_100_EXCEPTION)
                        .toString();
            } else {
                return new JsonReInfo(102, ExceptionCode.CODE_102_EXCEPTION)
                        .toString();
            }
        } catch (Exception e) {
            throw new JsonReException(101, ExceptionCode.CODE_101_EXCEPTION, e);
        }
    }
}
